/*
 * AcessoBD.java
 *
 * Created on 13 de Dezembro de 2006, 22:29
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package magchess10S.ClassesPE;

import java.io.Serializable;
import java.sql.*;

public class AcessoBD implements Serializable {
    
    private static Connection c_connection;
    public AcessoBD() {
    }
    private void abreConexao() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            c_connection = DriverManager.getConnection("jdbc:odbc:BANCO","","");
        } catch(Exception e) {
            e.printStackTrace();
            e.toString();
        }
        
    }
    
    
    
    public boolean consultaBD(String p_apelidoJogador,String p_senhaJogador) {
        boolean l_existeCadastro =false;
        abreConexao();
        try {
            Statement l_statement = c_connection.createStatement();
            String l_comandoSqlConsulta = "SELECT * FROM tabelaAcesso WHERE Apelido='"+p_apelidoJogador+"' AND Senha ='"+p_senhaJogador+"'";
            ResultSet l_result = l_statement.executeQuery(l_comandoSqlConsulta);
            if (l_result.next())
                l_existeCadastro = true;
            l_statement.close();
            c_connection.close();
        }
        
        
        catch (Exception e){
            e.printStackTrace();
            e.toString();}
        return l_existeCadastro;
    }
    
    private boolean ConsultaRegistroExistente(String p_apelidoJogador) {
        boolean l_existeCadastro =false;
        abreConexao();
        try {
            
            Statement l_statement = c_connection.createStatement();
            String l_comandoSqlConsulta = "SELECT * FROM tabelaAcesso WHERE Apelido='"+p_apelidoJogador+"'";
            ResultSet l_result = l_statement.executeQuery(l_comandoSqlConsulta);
            if (l_result.next())
                l_existeCadastro = true;
            l_statement.close();
           
        } catch (Exception e){
            e.printStackTrace();
            e.toString();}
        return l_existeCadastro;
    }
    
    
    public boolean InserirBD(String p_apelido, String p_senha, String p_primeiroNome,String p_ultimoNome , String p_email) {
        abreConexao();
        if (!ConsultaRegistroExistente(p_apelido)) {
            try {
                boolean a;
                Statement  l_statement = c_connection.createStatement();
                String l_comandoSqlInsert = "INSERT INTO tabelaAcesso VALUES ('"+ p_apelido + "','"+p_senha+"','"+p_primeiroNome+"','"+p_ultimoNome+"','"+p_email+"')";
                ResultSet rs=l_statement.executeQuery(l_comandoSqlInsert);
                if (rs.next())
                    a = true;
                l_statement.close();
                c_connection.close();
            } catch (Exception e){
                e.printStackTrace();
                e.toString();}
        } else
            return false;
        return true;
    }
}


